﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;
using Tp.Dto.Allgemein;
using Tp.Dto.Enums;

namespace Tp.Dto.Stammdaten
{
	  public class PersonDto : InPcBase, IAbstractDto
	  {

			#region Members

			private bool isDirty = false;
			private bool hasValue;
			private bool canUndo = false;

			private string vorname;
			private string vornameOld;
			private string nachname;
			private string nachnameOld;
			private ClientEnums.Sex geschlecht;
			private ClientEnums.Sex geschlechtOld;
			private TitleDto titel1;
			private TitleDto titel1Old;
			private TitleDto titel2;
			private TitleDto titel2Old;
			private TitleDto titel3;
			private TitleDto titel3Old;
			private AddressDto adresse;
			private AddressDto adresseOld;
			private TelefonDto mobil;
			private TelefonDto mobilOld;
			private TelefonDto privatTelefon;
			private TelefonDto privatTelefonOld;
			private TelefonDto privatFax;
			private TelefonDto privatFaxOld;
			private string notiz;
			private string notizOld;
			private bool mitarbeiter;
			private bool mitarbeiterOld;
			private string initials;
			private string initialsOld;
			private string email;
			private string emailOld;

			#endregion

			#region Properties

			[DataMember]
			public int PersonDtoId { get; set; }

			[DataMember]
			public string Vorname
			{
				  get { return this.vorname; }
				  set
				  {
						if (this.vorname == value)
						{
							  return;
						}

						if (this.vorname != value)
						{
							  this.IsDirty = true;
						}
						this.vornameOld = this.vorname;
						this.vorname = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => Vorname);
				  }
			}

			[DataMember]
			public string Nachname
			{
				  get { return this.nachname; }
				  set
				  {
						if (this.nachname == value)
						{
							  return;
						}

						if (this.nachname != value)
						{
							  this.IsDirty = true;
						}
						this.nachnameOld = this.nachname;
						this.nachname = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => Nachname);
				  }
			}

			[DataMember]
			public ClientEnums.Sex Geschlecht
			{
				  get { return this.geschlecht; }
				  set
				  {
						if (this.geschlecht == value)
						{
							  return;
						}

						if (this.geschlecht != value)
						{
							  this.IsDirty = true;
						}
						this.geschlechtOld = this.geschlecht;
						this.geschlecht = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => Geschlecht);
				  }
			}

			[DataMember]
			public TitleDto Titel1
			{
				  get { return this.titel1; }
				  set
				  {
						if (this.titel1 == value)
						{
							  return;
						}

						if (this.titel1 != value)
						{
							  this.IsDirty = true;
						}
						this.titel1Old = this.titel1;
						this.titel1 = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => Titel1);
				  }
			}

			[DataMember]
			public TitleDto Titel2
			{
				  get { return this.titel2; }
				  set
				  {
						if (this.titel2 == value)
						{
							  return;
						}

						if (this.titel2 != value)
						{
							  this.IsDirty = true;
						}
						this.titel2Old = this.titel2;
						this.titel2 = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => Titel2);
				  }
			}

			[DataMember]
			public TitleDto Titel3
			{
				  get { return this.titel3; }
				  set
				  {
						if (this.titel3 == value)
						{
							  return;
						}

						if (this.titel3 != value)
						{
							  this.IsDirty = true;
						}
						this.titel3Old = this.titel3;
						this.titel3 = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => Titel3);
				  }
			}

			[DataMember]
			public AddressDto Adresse
			{
				  get { return this.adresse; }
				  set
				  {
						if (this.adresse == value)
						{
							  return;
						}

						if (this.adresse != value)
						{
							  this.IsDirty = true;
						}
						this.adresseOld = this.adresse;
						this.adresse = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => Adresse);
				  }
			}

			[DataMember]
			public TelefonDto Mobil
			{
				  get { return this.mobil; }
				  set
				  {
						if (this.mobil == value)
						{
							  return;
						}

						if (this.mobil != value)
						{
							  this.IsDirty = true;
						}
						this.mobilOld = this.mobil;
						this.mobil = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => Mobil);
				  }
			}

			[DataMember]
			public TelefonDto PrivatTelefon
			{
				  get { return this.privatTelefon; }
				  set
				  {
						if (this.privatTelefon == value)
						{
							  return;
						}

						if (this.privatTelefon != value)
						{
							  this.IsDirty = true;
						}
						this.privatTelefonOld = this.privatTelefon;
						this.privatTelefon = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => PrivatTelefon);
				  }
			}

			[DataMember]
			public TelefonDto PrivatFax
			{
				  get { return this.privatFax; }
				  set
				  {
						if (this.privatFax == value)
						{
							  return;
						}

						if (this.privatFax != value)
						{
							  this.IsDirty = true;
						}
						this.privatFaxOld = this.privatFax;
						this.privatFax = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => PrivatFax);
				  }
			}

			[DataMember]
			public string Notiz
			{
				  get { return this.notiz; }
				  set
				  {
						if (this.notiz == value)
						{
							  return;
						}

						if (this.notiz != value)
						{
							  this.IsDirty = true;
						}
						this.notizOld = this.notiz;
						this.notiz = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => Notiz);
				  }
			}

			[DataMember]
			public bool Mitarbeiter
			{
				  get { return this.mitarbeiter; }
				  set
				  {
						if (this.mitarbeiter == value)
						{
							  return;
						}

						if (this.mitarbeiter != value)
						{
							  this.IsDirty = true;
						}
						this.mitarbeiterOld = this.mitarbeiter;
						this.mitarbeiter = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => Mitarbeiter);
				  }
			}

			[DataMember]
			public string Initials
			{
				  get { return this.initials; }
				  set
				  {
						if (this.initials == value)
						{
							  return;
						}

						if (this.initials != value)
						{
							  this.IsDirty = true;
						}
						this.initialsOld = this.initials;
						this.initials = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => Initials);
				  }
			}

			[DataMember]
			public string eMail
			{
				  get { return this.email; }
				  set
				  {
						if (this.email == value)
						{
							  return;
						}

						if (this.email != value)
						{
							  this.IsDirty = true;
						}
						this.emailOld = this.email;
						this.email = value;
						this.CanUndo = true;
						RaisePropertyChanged(() => eMail);
				  }
			}

			[DataMember]
			public List<OrganisationDto> PersonCompanys { get; set; }

			public bool HasValue
			{
				  get { return Nachname != null; }
			}

			public bool IsDirty
			{
				  get { return this.isDirty; }
				  set
				  {
						this.isDirty = value;
						RaisePropertyChanged(() => IsDirty);
				  }
			}

			public bool CanUndo
			{
				  get { return this.canUndo; }
				  set
				  {
						this.canUndo = value;
						RaisePropertyChanged(() => CanUndo);
				  }
			}

			#endregion

			#region C-Tor

			public PersonDto(
				  int personDtoId,
				  string vorname,
				  string nachname,
				  ClientEnums.Sex geschlecht,
				  TitleDto titel1,
				  TitleDto titel2,
				  TitleDto titel3,
				  AddressDto adresse,
				  TelefonDto mobil,
				  TelefonDto privatTelefon,
				  TelefonDto privatFax,
				  string notiz,
				  bool mitarbeiter,
				  string initials,
				  string email
)
			{
				  this.PersonDtoId = personDtoId;
				  this.Vorname = vorname;
				  this.Nachname = nachname;
				  this.Geschlecht = geschlecht;
				  this.Titel1 = titel1;
				  this.Titel2 = titel2;
				  this.Titel3 = titel3;
				  this.Adresse = adresse;
				  this.Mobil = mobil;
				  this.PrivatTelefon = privatTelefon;
				  this.PrivatFax = privatFax;
				  this.Notiz = notiz;
				  this.Mitarbeiter = mitarbeiter;
				  this.Initials = initials;
				  this.eMail = email;

				  if (Mobil != null)
				  {
						this.Mobil.PropertyChanged += OnPropertyChanged;
				  }
				  if (Titel1 != null)
				  {
						this.Titel1.PropertyChanged += OnPropertyChanged;
				  }
				  if (Titel2 != null)
				  {
						this.Titel2.PropertyChanged += OnPropertyChanged;
				  }
				  if (Titel3 != null)
				  {
						this.Titel3.PropertyChanged += OnPropertyChanged;
				  }
				  if (PrivatTelefon != null)
				  {
						this.PrivatTelefon.PropertyChanged += OnPropertyChanged;
				  }
				  if (PrivatFax != null)
				  {
						this.PrivatFax.PropertyChanged += OnPropertyChanged;
				  }
				  if (Adresse != null)
				  {
						this.Adresse.PropertyChanged += OnPropertyChanged;
				  }
			}

			#endregion

			#region Methods

			private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
			{
				  if (e.PropertyName == "Plz" || e.PropertyName == "Ort" || e.PropertyName == "Strasse" || e.PropertyName == "Zone" || e.PropertyName == "Entfernung" || e.PropertyName == "Hausnummer")
				  {
						this.CanUndo = true;
						this.IsDirty = true;
						RaisePropertyChanged(() => Adresse);
				  }
				  if (e.PropertyName == "Vorwahl" || e.PropertyName == "TelefonNummer" || e.PropertyName == "Durchwahl")
				  {
						this.CanUndo = true;
						this.IsDirty = true;
						RaisePropertyChanged(() => PrivatTelefon);
						RaisePropertyChanged(() => Mobil);
						RaisePropertyChanged(() => PrivatFax);
				  }
				  if (e.PropertyName == "Name")
				  {
						this.CanUndo = true;
						this.IsDirty = true;
						RaisePropertyChanged(() => Titel1);
						RaisePropertyChanged(() => Titel2);
						RaisePropertyChanged(() => Titel3);
				  }
				  
			}

			public void UndoChanges()
			{
				  this.Vorname = (this.vornameOld == null ? this.Vorname : this.vornameOld);
				  this.Nachname = (this.nachnameOld == null ? this.Nachname : this.nachnameOld);
				  this.Geschlecht = (this.geschlechtOld == null ? this.Geschlecht : this.geschlechtOld);
				  this.Titel1.UndoChanges();
				  this.Titel2.UndoChanges();
				  this.Titel3.UndoChanges();
				  this.Adresse.UndoChanges();
				  this.Mobil.UndoChanges();
				  this.PrivatTelefon.UndoChanges();
				  this.PrivatFax.UndoChanges();
				  this.Notiz = (this.notizOld == null ? this.Notiz : this.notizOld);
				  this.Mitarbeiter = (this.mitarbeiterOld == null ? this.Mitarbeiter : this.mitarbeiterOld);
				  this.Initials = (this.initialsOld == null ? this.Initials : this.initialsOld);
				  this.eMail = (this.emailOld == null ? this.eMail : this.emailOld);
			}

			#endregion

	  }
}
